package com.cms.web.business;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.cms.biz.business.CMSGeneralContractServiceMgr;
import com.cms.biz.common.EMPContractUtil;
import com.cms.biz.dict.CMSContractServiceMgr;
import com.cms.persistence.contractManager.generalContract.HTFKXX;
import com.cms.persistence.contractManager.generalContract.HTXXZD;
import com.cms.persistence.contractManager.generalContract.HTZDMX;
import com.common.util.DateUtil;
import com.dal.common.sequence.UUIDGenerator;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.qxgl.beans.SYSUser;
import com.pub.util.FileUtil;
import com.pub.util.StringUtil;
import com.web.BaseController;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.builder.util.EMPRowSetUtil;
import com.efounder.dict.DMTServiceMgr;
import com.efounder.dict.FCTServiceMgr;
import com.efounder.eai.data.JParamObject;
import com.efounder.eai.data.JResponseObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.server.provider.FormModelDataProvider;
import com.efounder.form.server.resolver.util.FormModelDataResolverUtil;

/** 
 * 一般合同控制器
 * 
 * @author ES
 * @version 1.0
 */
@Controller
@RequestMapping(value="/contractManager")
public class GeneralContractController extends BaseController{
	@Autowired
	private                            DMTServiceMgr                        DMTService;
	
	@Autowired
	private                            FCTServiceMgr                        FCTService;
	
	@Autowired
	private                    CMSContractServiceMgr             CMSContractServiceMgr;

	@Autowired
	private                    FormModelDataProvider                     formModelUtil;
	
	@Autowired
	private                FormModelDataResolverUtil                 formModelResolver;
	
	@Autowired
	private             CMSGeneralContractServiceMgr                generalContractMgr;
	
	@Autowired
	public                               HttpSession                           session;
	@Autowired
	public                           EMPContractUtil                      contractUtil;
	/**
	 * 合同信息列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/generalContract")
	public String generalContractList(@RequestParam String F_DJLX, Model model) {	
		JParamObject           PO = JParamObject.Create();
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		PO.SetValueByParamName("TABLE_NAME", "HTXXZD");
		PO.SetValueByParamName("F_DJLX", F_DJLX);
		PO.SetValueByParamName("sqlWhere", " F_DJLX = '" + F_DJLX + "' and F_BMBH = '" + user.getUSER_ORGID() + "'");
		PO.SetValueByParamName("sqlOrderBy", " HTXXZD.F_HTBH desc");
		EFDataSet dataset = generalContractMgr.loadCMSGeneralContract(PO);
		model.addAttribute("dataset", dataset);
		model.addAttribute("F_DJLX", F_DJLX);
		return "/cms/business/generalContract/htxx/list";
	}
	
	/**
	 * 合同信息列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/generalContract/list")
	public String generalContractList(@RequestParam String F_DJLX, @RequestParam String F_HTMC, @RequestParam String F_XMMC, Model model) {	
		JParamObject           PO = JParamObject.Create();
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		
		String sqlWhere = " HTXXZD.F_HTMC like '" + F_HTMC + "%' and HTXXZD.F_XMMC like '" + F_XMMC + "%' and F_DJLX = '" + F_DJLX + "' and F_BMBH = '" + user.getUSER_ORGID() + "'";
		
		PO.SetValueByParamName("TABLE_NAME", "HTXXZD");
		PO.SetValueByParamName("sqlWhere", sqlWhere);
		PO.SetValueByParamName("sqlOrderBy", " HTXXZD.F_HTBH desc");
		
		EFDataSet dataset = generalContractMgr.loadCMSGeneralContract(PO);
		model.addAttribute("dataset", dataset);
		model.addAttribute("F_HTMC", F_HTMC);
		model.addAttribute("F_XMMC", F_XMMC);
		model.addAttribute("F_DJLX", F_DJLX);
		return "/cms/business/generalContract/htxx/list";
	}
	
	/**
	 * 合同信息添加
	 * @param model
	 * @return
	 */
	@RequestMapping("/generalContract/add")
	public String generalContractAdd(@RequestParam String F_DJLX, Model model) {	
		JParamObject             PO = JParamObject.Create();
		String               tempBh = "";
		Date                   date = new Date();
		SimpleDateFormat  formatter = new SimpleDateFormat("yyyy-MM-dd");
		String              strDate = formatter.format(date);
		EFRowSet             rowset = EFRowSet.getInstance();
		
		PO.SetValueByParamName("F_DJLX", F_DJLX);
		tempBh = DMTService.getTempDictBM(PO, "HTXXZD");
		
		rowset.putString("F_HTBH", tempBh);
		rowset.putString("F_QYSJ", strDate);
		rowset.putString("F_QDSJ", strDate);
		rowset.putString("F_JHRQ", strDate);
		rowset.putString("F_DJLX", F_DJLX);
		rowset.putString("F_GUID", UUIDGenerator.nextId());
		model.addAttribute("dictObject", rowset);
		return "/cms/business/generalContract/htxx/add";
	}
	
	/**
	 * 合同信息添加保存
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/generalContract/insert", method = RequestMethod.POST)
	public ModelAndView generalContractInsert(HTXXZD htxx) {	
		EFRowSet             rowset;
		JResponseObject          RO = null;
		JParamObject             PO = JParamObject.Create();
		SYSUser                user = (SYSUser) session.getAttribute("contextUser");
		Date                   date = new Date();
		SimpleDateFormat  formatter = new SimpleDateFormat("yyyy");
		
		try {
			rowset = EMPRowSetUtil.createRowSet(htxx);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxDoneError(e.getMessage());
		}
		rowset.putString("F_BMBH", user.getUSER_ORGID());
		PO.SetValueByParamName("F_DJLX", htxx.getF_DJLX());
		PO.SetValueByParamName("F_DATE", formatter.format(date));
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "HTXXModel");
		PO.SetValueByParamName(SYS_MODEL._MDL_BHDCT_, "HTDJLX");
		PO.SetValueByParamName(SYS_MODEL._MDL_BHDCT_ + "_VALUE", htxx.getF_DJLX());
		RO = DMTService.insertMDLRow(PO, "HTXXZD", rowset);
		if(RO.getErrorCode() == -1) {
			return ajaxDoneError(RO.getErrorString());
		} else {
			return ajaxDoneSuccess(RO.ResponseObject.toString());
		}
	}
	
	/**
	 * 合同信息编辑
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/generalContract/edit/{F_ZDBH}")
	public String generalContractEdit(@PathVariable("F_ZDBH") String F_ZDBH, @RequestParam String F_DJLX, Model model) throws Exception {	
		double               htje = 0.0;
		double               ljje = 0.0;
		double                 je = 0.0;
		JParamObject           PO = JParamObject.Create();
		EFRowSet           rowset = DMTService.getRow("HTXXZD", "F_GUID", F_ZDBH);
		EFDataSet         dataset = CMSContractServiceMgr.getHTZDMXDataSetRow(null, rowset.getString("F_HTBH", ""));
		EFRowSet         htRowSet = contractUtil.getYBHT(rowset.getString("F_HTBH", ""));

		htje = rowset.getNumber("F_HTJE", Double.valueOf(String.valueOf("0.0"))).doubleValue();
		ljje = htRowSet.getNumber("F_LJJE", Double.valueOf(String.valueOf("0.0"))).doubleValue();
		je = htje - ljje;
		rowset.putString("F_YFK", String.valueOf(ljje));
		rowset.putString("F_WFK", String.valueOf(je));
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "SCTZModel");
		PO.SetValueByParamName("BILL_WHERE", " ####.F_HTBH = '" + rowset.getString("F_HTBH", "") + "'");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormEnumProvider");
		EFFormDataModel formDataModel = formModelUtil.loadForm(PO);
		model.addAttribute("sctzdDataSet", formDataModel.getBillDataSet());
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "CCTZModel");
		PO.SetValueByParamName("BILL_WHERE", " ####.F_HTBH = '" + rowset.getString("F_HTBH", "") + "'");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormEnumProvider");
		formDataModel = formModelUtil.loadForm(PO);
		model.addAttribute("cctzdDataSet", formDataModel.getBillDataSet());
		
		model.addAttribute("dictObject", rowset);
		model.addAttribute("dataset", dataset);
		return "/cms/business/generalContract/htxx/edit";
	}
	
	/**
	 * 合同信息编辑保存
	 * @param model
	 * @return
	 */
	@RequestMapping("/generalContract/update")
	public ModelAndView generalContractUpdate(HTXXZD htxx, Model model) {
		EFRowSet rowset = null;
		try {
			rowset = EMPRowSetUtil.createRowSet(htxx);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxDoneError(e.getMessage());
		}
		rowset.putString("F_HTBH", htxx.getF_HTBH());
		JResponseObject RO = DMTService.updateRow("HTXXZD", rowset);
		if(RO.getErrorCode() == -1) {
			return ajaxDoneError(RO.getErrorString());
		} else {
			return ajaxDoneSuccess(RO.ResponseObject.toString());
		}
	}
	
	/**
	 * 合同信息删除
	 * @param model
	 * @return
	 */
	@RequestMapping("/generalContract/delete/{F_ZDBH}")
	public ModelAndView generalContractDelete(@PathVariable("F_ZDBH") String F_ZDBH) {	
		JResponseObject RO = DMTService.deleteRow("HTXXZD", F_ZDBH);
		if(RO.getErrorCode() == -1) {
			return ajaxDoneError(RO.getErrorString());
		} else {
			return ajaxDoneSuccess(RO.ResponseObject.toString());
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 合同信息列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/generalContractDetails/list/{F_HTBH}")
	public String contractDetailsList(@PathVariable String F_HTBH, Model model) {	
		EFDataSet dataset = CMSContractServiceMgr.getHTZDMXDataSetRow(null, F_HTBH);
		EFRowSet   rowset = EFRowSet.getInstance();
		rowset.putString("F_HTBH", F_HTBH);
		model.addAttribute("dictObject", rowset);
		model.addAttribute("dataset", dataset);
		return "/cms/business/generalContract/htmx/list";
	}
	
	/**
	 * 合同信息添加
	 * @param model
	 * @return
	 */
	@RequestMapping("/generalContractDetails/add")
	public String generalContractDetailsAdd(@RequestParam String F_HTBH, Model model) {
		long                time = new Date().getTime();
		model.addAttribute("F_HTBH", F_HTBH);
		model.addAttribute("F_FLBH", time + UUIDGenerator.nextId());
		return "/cms/business/generalContract/htmx/add";
	}
	
	/**
	 * 合同信息添加保存
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/generalContractDetails/insert", method = RequestMethod.POST)
	public ModelAndView generalContractDetailsInsert(HTZDMX htzdmx) {	
		EFRowSet rowset;
		try {
			rowset = EMPRowSetUtil.createRowSet(htzdmx);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxDoneError(e.getMessage());
		}
		JResponseObject RO = FCTService.insertFCTRow("HTZDMX", rowset);
		if(RO.getErrorCode() == -1) {
			return ajaxDoneError(RO.getErrorString());
		} else {
			return ajaxDoneSuccess(RO.ResponseObject.toString());
		}
	}
	
	/**
	 * 合同明细编辑
	 * @param model
	 * @return
	 */
	@RequestMapping("/generalContractDetails/edit/{F_FLBH}")
	public String generalContractDetailsEdit(@PathVariable String F_FLBH, Model model) {
		EFDataSet dataset = CMSContractServiceMgr.getHTZDMXDataSetRow(F_FLBH, null);
		EFRowSet rowset = EFRowSet.getInstance();
		
		if(dataset.getRowCount() > 0) {
			rowset = dataset.getRowSet(0);
		}
		model.addAttribute("dictObject", rowset);
		return "/cms/business/generalContract/htmx/edit";
	}
	
	/**
	 * 合同产品编辑保存
	 * @param model
	 * @return
	 */
	@RequestMapping("/generalContractDetails/update")
	public ModelAndView contractProductUpdate(HTZDMX htzdmx, Model model) {
		EFRowSet rowset = null;
		try {
			rowset = EMPRowSetUtil.createRowSet(htzdmx);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxDoneError(e.getMessage());
		}
		JResponseObject RO = DMTService.updateRow("HTZDMX", rowset);
		if(RO.getErrorCode() == -1) {
			return ajaxDoneError(RO.getErrorString());
		} else {
			return ajaxDoneSuccess(RO.ResponseObject.toString());
		}
	}
	
	/**
	 * 合同产品删除
	 * @param model
	 * @return
	 */
	@RequestMapping("/generalContractDetails/delete/{F_ZDBH}")
	public ModelAndView generalContractDetailsDelete(@PathVariable("F_ZDBH") String F_ZDBH) {	
		JResponseObject RO = DMTService.deleteRow("HTZDMX", F_ZDBH);
		if(RO.getErrorCode() == -1) {
			return ajaxDoneError(RO.getErrorString());
		} else {
			return ajaxDoneSuccess(RO.ResponseObject.toString());
		}
	}

	//--------------------------------------------------------------------------------------------------
	//描述:项目申请单导入
	//设计: ES
	//实现: ES
	//修改:
	//--------------------------------------------------------------------------------------------------
	@RequestMapping("/generalContractDetails/importFile")
	public String importYBHEFormItemFile(@RequestParam String F_HTBH, Model model) {
		model.addAttribute("F_HTBH", F_HTBH);
		return "/cms/business/generalContract/htmx/importFile";
	}
	
	//--------------------------------------------------------------------------------------------------
	//描述:合同产品导入
	//设计: ES
	//实现: ES
	//修改:
	//--------------------------------------------------------------------------------------------------
	@RequestMapping("/generalContractDetails/importYBHTFormItem")
	public void generalContractDetailsimportYBHTFormItem(@RequestParam String F_HTBH, HttpServletRequest request, HttpServletResponse response, Model model) {
		String                 fileName;
		String              newFileName = null;
		String            fileSeparator = System.getProperty("file.separator");
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;    
		String                           servletPath = request.getRealPath("") + fileSeparator + "upload" + fileSeparator + "cms" + fileSeparator + "temp";
		String                             strFolder = "";
		Map<String, MultipartFile>           fileMap = multipartRequest.getFileMap();  
		java.util.Date                      currTime = new java.util.Date();
		SimpleDateFormat                   formatter = new SimpleDateFormat("yyyyMMdd");
		strFolder = formatter.format(currTime);		
		
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {    
             // 上传文件名    
             System.out.println("key: " + entity.getKey());    
             MultipartFile mf = entity.getValue();    
             fileName = mf.getOriginalFilename();           
             String uuid = StringUtil.nextId();// 返回一个随机UUID。
             String suffix = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf("."), fileName.length()) : null;

             newFileName = servletPath + fileSeparator + strFolder + fileSeparator  + uuid + (suffix!=null?suffix:"");// 构成新文件名。
             
             //判断目录是否存在，每天上传的申请单都放到对应的一个目录下
             FileUtil.createDirectory(servletPath + fileSeparator + strFolder);
             File uploadFile = new File(newFileName);    
             try {  
                 FileCopyUtils.copy(mf.getBytes(), uploadFile); 
	         } catch (IOException e) {  
	             e.printStackTrace();  
	         }    
		}   
		try {
			String forwardUrl = "&forwardUrl=" + request.getParameter("forwardUrl") + F_HTBH;
			response.getWriter().write(newFileName);
			response.getWriter().write("&F_HTBH=" + F_HTBH);
			response.getWriter().write(forwardUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//--------------------------------------------------------------------------------------------------
	//描述:项目申请单Excel文件解析
	//设计: ES
	//实现: ES
	//修改:
	//--------------------------------------------------------------------------------------------------
	@RequestMapping("/generalContractDetails/statementsReslove")
	public ModelAndView statementsReslove(@RequestParam String F_HTBH, @RequestParam String FILE_NAME, Model model) {
		File            file = new File(FILE_NAME);
		EFDataSet    dataset = null;
		
		if(!file.exists()) ajaxDoneError("解析失败，上传文件不存在！");  
		
		JResponseObject RO = statementsReslove(FILE_NAME);

		if(RO.getErrorCode() == -1) return ajaxDoneError(RO.getErrorString());
		
		dataset = (EFDataSet) RO.getResponseObject();
		
		for(int i = 0; i < dataset.getRowCount(); i++) {
			dataset.getRowSet(i).putString("F_HTBH", F_HTBH);
		}
		RO = FCTService.insertFCTDataSet("HTZDMX", dataset);
		if(RO.getErrorCode() == -1) {
			dataset = DMTService.searchDictRow("HTZDMX", "F_HTBH", F_HTBH, null, 0, 100);
			model.addAttribute("dataset", dataset);
			return ajaxDoneError(RO.getErrorString());
		}
		dataset = DMTService.searchDictRow("HTZDMX", "F_HTBH", F_HTBH, null, 0, 100);	
		model.addAttribute("dataset", dataset);
		return ajaxDoneSuccess(RO.getResponseObject().toString());
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 合同付款列表
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/contractPayment")
	public String contractPaymentList(@RequestParam String F_DJLX, Model model) throws Exception {	
		JParamObject           PO = JParamObject.Create();
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "HTFKModel");
		PO.SetValueByParamName("BILL_WHERE", " HTFKXX.F_BMBH = '" + user.getUSER_ORGID() + "' and HTFKXX.F_DJLX = '" + F_DJLX + "'");
		PO.SetValueByParamName("BILL_ORDER", " desc");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormEnumProvider");
		EFFormDataModel formDataModel = formModelUtil.loadForm(PO);
		model.addAttribute("dataset", formDataModel.getBillDataSet());
		model.addAttribute("F_DJLX", F_DJLX);
		return "/cms/business/generalContract/htfk/list";
	}
	
	/**
	 * 合同付款列表
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/contractPayment/list")
	public String contractPaymentList(@RequestParam String F_DJLX, @RequestParam String F_HTMC, @RequestParam String F_DJZT, Model model) throws Exception {
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		JParamObject           PO = JParamObject.Create();
		String         BILL_WHERE = "F_HTBH.F_HTMC like '" + F_HTMC + "%' and HTFKXX.F_BMBH = '" + user.getUSER_ORGID() + "' and HTFKXX.F_DJLX = '" + F_DJLX + "'";
		
		if(!F_DJZT.equals("-1")) {
			BILL_WHERE += " and HTFKXX.F_DJZT = '" + F_DJZT + "'";
		}
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "HTFKModel");
		PO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		PO.SetValueByParamName("BILL_ORDER", " desc");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormEnumProvider");
		EFFormDataModel formDataModel = formModelUtil.loadForm(PO);
		model.addAttribute("dataset", formDataModel.getBillDataSet());
		model.addAttribute("F_HTMC", F_HTMC);
		model.addAttribute("F_DJZT", F_DJZT);
		model.addAttribute("F_DJLX", F_DJLX);
		return "/cms/business/generalContract/htfk/list";
	}
	
	/**
	 * 合同付款添加
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/contractPayment/add")
	public String contractPaymentAdd(@RequestParam String F_DJLX, @RequestParam("F_GUID") String F_GUID, Model model) throws Exception {	
		SYSUser                user = (SYSUser) session.getAttribute("contextUser");
		JParamObject             PO = JParamObject.Create();
		EFRowSet         headRowSet = null;
		EFRowSet           htRowSet = null;
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "HTFKModel");
		PO.SetValueByParamName("BILL_WHERE", " HTFKXX.F_GUID = '" + F_GUID + "'");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormEnumProvider");
		EFFormDataModel formDataModel = formModelUtil.loadForm(PO);
		headRowSet = formDataModel.getBillDataSet().getRowSet(0);
		
		if(headRowSet == null || headRowSet.getString("F_DJBH", "").trim().length() == 0) {
			headRowSet = EFRowSet.getInstance();
			headRowSet.putString("F_FKCS", "1");
			headRowSet.putString("F_FKJE", "0.0");
			headRowSet.putString("F_DJZT", "0");
			headRowSet.putString("F_KJQJ", DateUtil.date3String("yyyyMM"));
			headRowSet.putString("F_ZDRMC", user.getUSER_NAME());
			headRowSet.putString("F_ZDR", user.getUSER_ID());
			headRowSet.putString("F_ZDRQ", DateUtil.date3String("yyyy-MM-dd"));
			headRowSet.putString("F_GUID", UUIDGenerator.nextId());
		} else {
			htRowSet = contractUtil.getYBHT(headRowSet.getString("F_HTBH", ""));
			headRowSet.putString("F_HTJE", String.valueOf(htRowSet.getObject("F_HTJE", "")));
			headRowSet.putString("F_LJJE", String.valueOf(htRowSet.getNumber("F_LJJE", Double.valueOf("0.0"))));
		}
		headRowSet.putString("F_BMBH", user.getUSER_ORGID());
		headRowSet.putString("F_DJLX", F_DJLX);
		model.addAttribute("dictObject", headRowSet);
		return "/cms/business/generalContract/htfk/add";
	}
	
	/**
	 * 合同付款添加保存
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "/contractPayment/insert", method = RequestMethod.POST)
	public ModelAndView contractPaymentInsert(HTFKXX htxx, Model model) throws Exception {	
		EFRowSet              rowset;
		JParamObject              PO = JParamObject.Create();
		String              F_CHDATE = "";
		String               strDate = DateUtil.date3String(DateUtil.PATTERN_DATE);
		SYSUser                 user = (SYSUser) session.getAttribute("contextUser");
		
		try {
			rowset = EMPRowSetUtil.createRowSet(htxx);
			rowset.putString("F_ZDRQ", strDate);
			rowset.putString("F_TJRQ", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxDoneError(e.getMessage());
		}
		rowset.putString("F_BMBH", user.getUSER_ORGID());
		rowset.putString(EFFormDataModel._FORMEDITSAVETYPE_, "" + EFFormDataModel._FORM_SAVE_ITEMS_ALL_);
		PO.SetValueByParamName("F_DJLX", htxx.getF_DJLX());
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "HTFKSaveResolver");
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "HTFKModel");
		EFFormDataModel formDataModel = formModelResolver.saveForm("HTFKXX", rowset, PO);
		if(formDataModel.getFormSaveStatus() == EFFormDataModel._FORM_SAVE_FAIL_) {
			return ajaxDoneError("保存失败！原因：<br>" + formDataModel.getFormSaveMessage());
		}
		rowset = formDataModel.getBillDataSet().getRowSet(0);
		F_CHDATE = rowset.getObject("F_CHDATE", "").toString();
		model.addAttribute("F_CHDATE", F_CHDATE);
		model.addAttribute("F_DJBH", rowset.getObject("F_DJBH", ""));
		model.addAttribute("F_GUID", rowset.getObject("F_GUID", ""));
		return ajaxDoneSuccess("单据保存成功！单据编号【" + rowset.getObject("F_DJBH", "") + "】"); 
	}
	
	/**
	 * 合同付款编辑
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/contractPayment/edit/{F_ZDBH}")
	public String contractPaymentEdit(@RequestParam String F_DJLX, @PathVariable("F_ZDBH") String F_ZDBH, Model model) throws Exception {
		JParamObject             PO = JParamObject.Create();
		EFRowSet           htRowSet = null;
		EFRowSet         headRowSet = null;
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "HTFKModel");
		PO.SetValueByParamName("BILL_WHERE", " HTFKXX.F_GUID = '" + F_ZDBH + "'");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormEnumProvider");
		EFFormDataModel formDataModel = formModelUtil.loadForm(PO);
		headRowSet = formDataModel.getBillDataSet().getRowSet(0);
		htRowSet = contractUtil.getYBHT(headRowSet.getString("F_HTBH", ""));
		headRowSet.putString("F_HTJE", String.valueOf(htRowSet.getObject("F_HTJE", "")));
		headRowSet.putString("F_LJJE", String.valueOf(htRowSet.getNumber("F_LJJE", Double.valueOf("0.0"))));
		model.addAttribute("dictObject", headRowSet);
		return "/cms/business/generalContract/htfk/edit";
	}
	
	/**
	 * 评审合同添加保存
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/contractPayment/submit", method=RequestMethod.POST)
	public  ModelAndView accreditationContractSubmit(HTFKXX htxx, Model model) throws Exception {  
		EFRowSet              rowset = EFRowSet.getInstance();
		String              F_CHDATE = "";
		JParamObject              PO = JParamObject.Create();		
		String               strDate = DateUtil.date3String(DateUtil.PATTERN_DATE);
		SYSUser                  user = (SYSUser) session.getAttribute("contextUser");
				
		try {
			rowset = EMPRowSetUtil.createRowSet(htxx);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxDoneError(e.getMessage());
		}
		
		rowset.putString("F_TJR", user.getUSER_ID());
		rowset.putString("F_TJRQ", strDate);
		rowset.putString("F_DJZT", "1");
		rowset.putString("F_BMBH", user.getUSER_ORGID());
		rowset.putString(EFFormDataModel._FORMEDITSAVETYPE_, "" + EFFormDataModel._FORM_SAVE_ITEMS_ALL_);
		
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "HTFKSubmitResolver");
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "HTFKModel");
		EFFormDataModel formDataModel = formModelResolver.submitForm("HTFKXX", rowset, PO);
		if(formDataModel.getFormSaveStatus() == EFFormDataModel._FORM_SAVE_FAIL_) {
			return ajaxDoneError("提交失败！原因：<br>" + formDataModel.getFormSaveMessage());
		}
		rowset = formDataModel.getBillDataSet().getRowSet(0);
		F_CHDATE = rowset.getObject("F_CHDATE", "").toString();
		model.addAttribute("F_CHDATE", F_CHDATE);
		model.addAttribute("F_DJBH", rowset.getObject("F_DJBH", ""));
		model.addAttribute("F_TJR", user.getUSER_ID());
		model.addAttribute("F_TJRQ", strDate);
		return ajaxDoneSuccess("单据分录提交成功！单据编号【" + rowset.getObject("F_DJBH", "") + "】"); 
	}
	
	/**
	 * 合同付款编辑保存
	 * @param model
	 * @return
	 */
	@RequestMapping("/contractPayment/update")
	public ModelAndView contractPaymentUpdate(HTFKXX fkxx, Model model) {
		EFRowSet rowset = null;
		try {
			rowset = EMPRowSetUtil.createRowSet(fkxx);
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxDoneError(e.getMessage());
		}
		rowset.putString("F_HTBH", fkxx.getF_HTBH());
		JResponseObject RO = DMTService.updateRow("HTFKXX", rowset);
		if(RO.getErrorCode() == -1) {
			return ajaxDoneError(RO.getErrorString());
		} else {
			return ajaxDoneSuccess(RO.ResponseObject.toString());
		}
	}
	
	/**
	 * 合同付款删除
	 * @param model
	 * @return
	 */
	@RequestMapping("/contractPayment/delete/{F_ZDBH}")
	public ModelAndView contractPaymentDelete(@PathVariable("F_ZDBH") String F_ZDBH) {	
		JResponseObject RO = DMTService.deleteRow("HTXXZD", F_ZDBH);
		if(RO.getErrorCode() == -1) {
			return ajaxDoneError(RO.getErrorString());
		} else {
			return ajaxDoneSuccess(RO.ResponseObject.toString());
		}
	}
	
	public JResponseObject statementsReslove(String FILE_NAME) {
		XSSFSheet          sheet = null;
		EFRowSet          rowset = null;
		String            F_GUID = "";
		XSSFWorkbook         xwb = null;
		JResponseObject       RO = new JResponseObject();	
		EFDataSet        dataset = null;
		long                time = new Date().getTime();
		try {
			xwb = new XSSFWorkbook(FILE_NAME);
			sheet = xwb.getSheetAt(0);    
			dataset = resloveItem(sheet);
			for(int i = 0; i < dataset.getRowCount(); i++) {
				rowset = dataset.getRowSet(i);
				F_GUID = time + UUIDGenerator.nextId();
				rowset.putString("F_FLBH", F_GUID);
				time++;
			}
		} catch (Exception ce) {
			RO.setErrorCode(-1);
			RO.setErrorString("文件导入失败:\r\n" + ce.getMessage());
			ce.printStackTrace();
			return RO;
		}
		RO.setErrorCode(0);
		RO.setResponseObject(dataset);
		RO.setErrorString("文件导入成功！");
		return RO;
	}
	
	/**  
     * @Method: extractTextFromXLS2007  
     * @Description: 从excel 2007文档中提取项目申请单明细信息 
     *  
     * @param   
     * @return String  
     * @throws  
     */  
    public EFDataSet resloveItem(XSSFSheet sheet) {
		EFDataSet dataset = EFDataSet.getInstance("HTZDMX");
		EFRowSet   rowset = EFRowSet.getInstance();
    	String     F_XMMC;
    	String     F_WZBM;
    	double         dj = 0.0;
    	double         sl = 0.0;
    	
    	for(int i = 0; true; i++) {
    		F_WZBM = getCellString(sheet, i, 0, "");
    		if(F_WZBM.equals("")) break;
    		F_XMMC = getCellString(sheet, i, 1, "");
    		if(F_XMMC.equals("")) break;
    		rowset = EFRowSet.getInstance();
    		rowset.putString("F_WZBM", F_WZBM);
    		rowset.putString("F_CPMC", F_XMMC);
    		rowset.putString("F_GGXH", getCellString(sheet, 2, 1, ""));
    		dj = getCellNumber(sheet, i, 3, 0);
    		sl = getCellNumber(sheet, i, 4, 0);
    		rowset.putNumber("F_DJ", dj);
    		rowset.putNumber("F_SL", sl);    		
    		dataset.addRowSet(rowset);
    	}
    	return dataset;
	}
    
    private String getCellString(XSSFSheet sheet, int row, int col, String defVal) {
    	int cellType = 0;
    	try {
	    	if(sheet.getRow(row) == null) return defVal;
	    	XSSFCell cell = sheet.getRow(row).getCell(col);
	    	if(cell == null) return defVal;
	    	cellType = cell.getCellType();
	    	if(cellType == 0) return "" + cell.getNumericCellValue();
	    	else {
	    		if(cell.getStringCellValue() == null) return defVal;
		    	else return cell.getStringCellValue().trim();
	    	}
    	} catch(Exception ce) {
    		ce.printStackTrace();
    	}
    	return defVal;
    }
    
    private double getCellNumber(XSSFSheet sheet, int row, int col, double defVal) {
    	int cellType = 0;
    	String   val = "";
    	double  dVal = 0.0;
    	try {
	    	XSSFCell cell = sheet.getRow(row).getCell(col);
	    	if(cell == null) return defVal;
	    	cellType = cell.getCellType();
	    	if(cellType == 0) {
	    		return cell.getNumericCellValue();	    		
	    	}
	    	else if(cellType == 1) {
	    		val = cell.getStringCellValue().trim();
	    		dVal = Double.parseDouble(val);
	    		return dVal;
	    	}
    	} catch(Exception ce) {
    		ce.printStackTrace();
    	}
    	return defVal;
    }
}
